﻿namespace EFS.RC.Survey
{
	public partial class ScoreExternal : EFS.RC.Survey.Controls.Common.Pages.Controls.Control
	{
		#region Constructors
		/// Implicit
		public ScoreExternal()
		{
			InitializeComponent();

			// do not use this
			ConstructorNotAllowed();
		}

		/// Explicit
		public ScoreExternal(
			System.Guid conversationGuid,
			System.Int32 surveyId)
		{
			// check
			EFS.Common.Verify.Guid(conversationGuid, "conversationGuid");
			EFS.Common.Verify.PositiveNumber(surveyId, "surveyId");

			// set assync handlers
			UtilitiesService.Survey_GetByIdCompleted += new System.EventHandler<EFS.RC.Survey.Controls.Utilities.Survey_GetByIdCompletedEventArgs>(UtilitiesService_Survey_GetByIdCompleted);

			// init componet
			InitializeComponent();

			// set
			this.ConversationGuid = conversationGuid;
			this.SurveyId = surveyId;

			// reset scoring
			labelScoring.Text = "";
		}
		#endregion

		#region Properties
		/// Property ConversationGuid: Get, Set
		public System.Guid ConversationGuid { get; private set; }

		/// Property SurveyId: Get, Set
		public System.Int32 SurveyId { get; private set; }
		#endregion

		#region LoadData
		/// LoadData
		private void LoadData()
		{
			// load survey object
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Loading Survey");

			// get survey
			UtilitiesService.Survey_GetByIdAsync(SurveyId);
		}
		#endregion

		#region Utilities
		/// HideSurvey
		private void HideSurvey()
		{
			dockPanelSurvey.Children.Clear();
			scrollViewerSurvey.Visibility = System.Windows.Visibility.Collapsed;
			gridScoring.Visibility = System.Windows.Visibility.Collapsed;
		}

		/// <summary>
		/// Show averagce chages
		/// </summary>
		/// <param name="s"></param>
		private void OnAverageChange(System.Double d)
		{
			gridScoring.Visibility = System.Windows.Visibility.Visible;
			labelScoring.Text = d.ToString("##.##");
		}

		/// <summary>
		/// Open survey in place 
		/// </summary>
		/// <param name="surver"></param>
		private void OpenSurvey(EFS.RC.CC.CH.Entities.Surveys.SurveyInfo surveyObj)
		{
			// hide previous
			HideSurvey();

			// set scroll to beginning
			scrollViewerSurvey.ScrollToVerticalOffset(0);

			// create new
			EFS.RC.Survey.Controls.Survey controlSurvey = new EFS.RC.Survey.Controls.Survey(surveyObj, ConversationGuid);
			controlSurvey.OnAverageChange += OnAverageChange;

			controlSurvey.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			controlSurvey.VerticalAlignment = System.Windows.VerticalAlignment.Top;
			controlSurvey.OnSaveCompleted = SurveyCompleted;
			controlSurvey.OnCancel = SurveyCanceled;
			dockPanelSurvey.Children.Add(controlSurvey);
			scrollViewerSurvey.Visibility = System.Windows.Visibility.Visible;
			
			// scoring
			gridScoring.Visibility = System.Windows.Visibility.Visible;
			labelScoring.Text = "";
		}

		// SurveyCompleted
		private void SurveyCompleted(System.Int32 surveySessionId)
		{
			// hide survey
			HideSurvey();

			// show message
			System.Windows.MessageBox.Show("Your score has been saved.", "Information", System.Windows.MessageBoxButton.OK);

			// close browser
			System.Windows.Browser.HtmlPage.Window.Invoke("close");
		}

		// SurveyCompleted
		private void SurveyCanceled()
		{
			// hide survey
			HideSurvey();

			// close browser
			System.Windows.Browser.HtmlPage.Window.Invoke("close");
		}
		#endregion

		#region WS callbacks
		/// Survey_GetByIdCompleted
		private void UtilitiesService_Survey_GetByIdCompleted(object sender, EFS.RC.Survey.Controls.Utilities.Survey_GetByIdCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Failed to get survey.", e.Error);
			}

			try
			{
				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo surveyObj = EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo>(e.Result.ToString());
				EFS.Common.Verify.Reference(surveyObj, "surveyObj");

				// open survey
				OpenSurvey(surveyObj);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to get survey." + ex.Message);
			}
		}
		#endregion

		#region Events
		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			LoadData();
		}
		#endregion
	}

}